package cn.tedu.boot04_1.controller;

import cn.tedu.boot04_1.entity.Product_1;
import cn.tedu.boot04_1.mapper.Product_1Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Product_1Controller {
    @Autowired
    Product_1Mapper mapper;
    @RequestMapping("/insert")
    public String insert(Product_1 product_1) {
        mapper.insert(product_1);
        return "添加完成!<a herf='/'>返回首页</a>";
    }
    @RequestMapping("/select")
    public String select() {
        List<Product_1> list = mapper.select();
        String html = "<table border=1>";
        html += "<caption>商品列表</caption>";
        html += "<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        for(Product_1 p1 : list) {
            html += "<tr>";
            html += "<td>" + p1.getId() + "</td>";
            html += "<td>" + p1.getTitle() + "</td>";
            html += "<td>" + p1.getPrice() + "</td>";
            html += "<td>" + p1.getNum() + "</td>";
            // html += <td><a href='/delete?id=p1.getId()'>删除</a></td>
            html += "<td><a href='/delete?id=" + p1.getId() + "'>删除</a></td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id) {
        mapper.deleteById(id);
        return "删除完成!<a href='/select'>返回列表首页</a>";

    }
    @RequestMapping("/update")
    public String update(Product_1 product_1) {
        mapper.update(product_1);
        return "修改完成!<a href='/select'>返回列表首页</a>";

    }

}
